package com.linkmed.storage.utils;



import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.BucketStorageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;

@Service      //1.主要用于标记配置类，兼备Component的效果。
public class BucketCapacityTask {

    @Value("${storage.oss.bucketName}")
    private  String bucketName;
    private Logger logger = LoggerFactory.getLogger(BucketCapacityTask.class);

   // @Scheduled(cron = "0 0/30 * * * ?")
    //@Scheduled(cron = "0 0 16 * * ?")
    @Scheduled(cron = "0 0 0/4 * * ?")
    public void scheduled() throws IOException {
        try {
            ObsClient obsClient = ObsClientFactory.getObsClient();
            BucketStorageInfo storageInfo = obsClient.getBucketStorageInfo(bucketName);
            BigDecimal bigDecimal = new BigDecimal(storageInfo.getSize());
            //BigDecimal bigDecimal = new BigDecimal("264375595017631");
            BigDecimal temp = bigDecimal.divide(new BigDecimal("1099511627776"),4,BigDecimal.ROUND_HALF_UP);
            logger.info("当前桶{}的文件数量为：{},占用空间{}T",bucketName,storageInfo.getObjectNumber(),temp);
        } catch (Exception e) {
            logger.error("统计桶数量，占用空间异常",e);
        }
    }
}


